package test.my.com.myapplicationtest.utils;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by ah on 2017/4/16.
 */

public class ViewUtil {

    /**
     * 获取view宽
     * @param view
     * @return
     */
    public static int getViewWidth(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width,height);
        return view.getMeasuredWidth();
    }

    /**
     * 获取view高
     * @param view
     * @return
     */
    public static int getViewHeight(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        return view.getMeasuredHeight();
    }

    /**
     * 获取屏幕宽高
     * @param context
     * @return wh[0]宽  wh[1]高
     */
    public static int[] getScreenWidthAndHeight(Context context) {
        int[] wh = new int[2];
        WindowManager wm1 = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wh[0] = wm1.getDefaultDisplay().getWidth();
        wh[1] = wm1.getDefaultDisplay().getHeight();
        return wh;
    }
}
